const dictDataService = require('../services/dictDataService');
const {success,error,successWithData} = require("../utils/resultwrapper")

// 获取字典数据分页列表
async function getDictDataPage(req, res) {
    try {
        const { pageNum, pageSize, dictCode, status, keywords } = req.query;
        const result = await dictDataService.getDictDataPage(parseInt(pageNum), parseInt(pageSize), dictCode, status, keywords);
        res.json(successWithData(result));
    } catch (error) {
        res.json(error(error.message));
    }
}

// 获取字典数据列表
async function getDictDataList(req, res) {
    try {
        const { dictCode, status, label } = req.query;
        const list = await dictDataService.getDictDataList(dictCode, status, label);
        res.json(successWithData(list));
    } catch (error) {
        res.json(error(error.message));
    }
}

// 根据ID获取字典数据详情
async function getDictDataById(req, res) {
    try {
        const { id } = req.params;
        const dictData = await dictDataService.getDictDataById(id);
        res.json(successWithData(dictData));
    } catch (error) {
        res.json(error(error.message));
    }
}

// 新增字典数据
async function createDictData(req, res) {
    try {
        const dictData = req.body;
        const result = await dictDataService.createDictData(dictData);
        res.json(successWithData(result));
    } catch (error) {
        res.json(error(error.message));
    }
}

// 更新字典数据
async function updateDictData(req, res) {
    try {
        const { id } = req.params;
        const dictData = req.body;
        const result = await dictDataService.updateDictData(id, dictData);
        res.json(successWithData(result));
    } catch (error) {
        res.json(error(error.message));
    }
}

// 删除字典数据
async function deleteDictData(req, res) {
    try {
        const { id } = req.params;
        await dictDataService.deleteDictData(id);
        res.json(success());
    } catch (error) {
        res.json(error(error.message));
    }
}

module.exports = {
    getDictDataPage,
    getDictDataList,
    getDictDataById,
    createDictData,
    updateDictData,
    deleteDictData
};